package cn.com.assistant.service.impl;

import cn.com.assistant.entity.AnswerEntity;
import cn.com.assistant.entity.CasAnswerEntity;
import cn.com.assistant.mapper.CasAnswerMapper;
import cn.com.assistant.model.ImageDTO;
import cn.com.assistant.service.CasAnswerService;
import cn.com.assistant.utils.ResponseMessage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class CasAnswerServiceImpl extends ServiceImpl<CasAnswerMapper, CasAnswerEntity> implements CasAnswerService {

    @Autowired
    CasAnswerMapper casAnswerMapper;

    @Override
    public ResponseMessage findByPid(String principalId) {
        List<CasAnswerEntity> casAnswerEntities = casAnswerMapper.findByPId(principalId);
        if (CollectionUtils.isNotEmpty(casAnswerEntities)) {
            for (int i = 0; i < casAnswerEntities.size(); i++) {
                CasAnswerEntity casAnswerEntity = casAnswerEntities.get(i);
                // 处理多张图片，根据分号处理成list
                List<String> imgUrlList = new ArrayList<>();
                if (StringUtils.isNotEmpty(casAnswerEntity.getImgUrl())) {
                    imgUrlList = Arrays.asList(casAnswerEntity.getImgUrl().split(";"));
                }
                casAnswerEntity.setImgUrlList(imgUrlList);
            }
        }
        return ResponseMessage.defaultResponse().setData(casAnswerEntities);
    }

    @Override
    public ResponseMessage update(CasAnswerEntity casAnswerEntity) {
        casAnswerEntity.setUpdatedDate(new Date());
        // 处理多张图片，追加分号
        List<String> imgUrlList = casAnswerEntity.getImgUrlList();
        if (CollectionUtils.isNotEmpty(imgUrlList)) {
            String imgUrl = imgUrlList.get(0) + ";";
            for (int j = 1; j < imgUrlList.size(); j++) {
                String url = imgUrlList.get(j) + ";";
                imgUrl += url;
            }
            casAnswerEntity.setImgUrl((imgUrl));
        } else {
            casAnswerEntity.setImgUrl((null));
        }
        int num = casAnswerMapper.update(casAnswerEntity);
        if (num == 1) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @Override
    public ResponseMessage updateImage(ImageDTO imageDTO) {
        CasAnswerEntity casAnswerEntity = new CasAnswerEntity();
        casAnswerEntity.setUpdatedDate(new Date());
        casAnswerEntity.setId(imageDTO.getId());
        // 处理多张图片，追加分号
        List<String> imgUrlList = imageDTO.getImgUrlList();
        if (CollectionUtils.isNotEmpty(imgUrlList)) {
            String imgUrl = imgUrlList.get(0) + ";";
            for (int j = 1; j < imgUrlList.size(); j++) {
                String url = imgUrlList.get(j) + ";";
                imgUrl += url;
            }
            casAnswerEntity.setImgUrl((imgUrl));
        } else {
            casAnswerEntity.setImgUrl((null));
        }

        int flag = casAnswerMapper.updateImage(casAnswerEntity);
        if (flag == 1) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }
}